<template>
  <div class="treeNoSelect">
    <a-input-search style="margin-bottom: 8px" placeholder="搜索" @change="onChange" />
    <a-tree
      :tree-data="treeData"
      show-icon
      default-expand-all
      :default-selected-keys="defaultSelectKeys"
      :expanded-keys="expandedKeys"
      :auto-expand-parent="autoExpandParent"
 
      @expand="onExpand"
    >
      <svg-icon slot="smile" style="font-size:23px;vertical-align: -0.33em;" icon-class="logo"></svg-icon>
      <template slot="title" slot-scope="{ title }">
        <span v-if="title.indexOf(searchValue) > -1">
          {{ title.substr(0, title.indexOf(searchValue)) }}
          <span
            style="color: #f50"
          >{{ searchValue }}</span>
          {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
        </span>
        <span v-else>{{ title }}</span>
      </template>
    </a-tree>
  </div>
</template>

<script>
const treeData = [
  {
    title: 'parent 1',
    key: '0-0',
    slots: {
      icon: 'smile'
    },
    scopedSlots:{
      title:'title'
    },
    children: [
      { title: 'leaf', key: '0-0-0',scopedSlots:{
      title:'title'
    }, slots: { icon: 'smile' } },
      { title: 'leaf', key: '0-0-1', scopedSlots:{
      title:'title'
    },slots: { icon: 'smile' } }
    ]
  }
]
const dataList = [];
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({ key, title: node.title });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(treeData);


const getParentKey = (key, tree) => {
  let parentKey
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i]
    if (node.children) {
      if (node.children.some((item) => item.key === key)) {
        parentKey = node.key
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children)
      }
    }
  }
  return parentKey
}
export default {
  name: 'treeNoSelect',
  data() {
    return {
      defaultSelectKeys: ['0-0-0'],
      treeData,
      expandedKeys: [],
      searchValue: '',
      autoExpandParent: true,
  
    }
  },
  mounted(){

    // console.log(dataList);
    
  },
  methods: {
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },

    onSelect(selectedKeys, info) {
      console.log('selected', selectedKeys, info)
    },
    onCheck(checkedKeys, info) {
      console.log('onCheck', checkedKeys, info)
    },

    //搜索树
    onChange(e) {
      const value = e.target.value
      // console.log(dataList);
      
      const expandedKeys = dataList
        .map((item) => {
          if (item.title.indexOf(value) > -1) {
              
            return getParentKey(item.key, this.treeData)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)
        // console.log(expandedKeys);
        
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true
      })
 
      
    }
  }
}
</script>

<style lang="less" scoped>
</style>
